package com.hunktimes.exam.service;

import com.hunktimes.exam.entity.Order;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 * 订单总价服务
 */
public class OrderTotalService extends OrderEngine {

    @Override
    protected BigDecimal doCalculate(List<Order> orderList) {
        BigDecimal result = BigDecimal.ZERO;
        for (Order order : orderList) {
            if (order == null) {
                continue;
            }
            BigDecimal price = Optional.ofNullable(order.getPrice()).orElse(BigDecimal.ZERO);
            int num = Optional.ofNullable(order.getNum()).orElse(0);
            BigDecimal money = price.multiply(BigDecimal.valueOf(num));
            result = result.add(money);
        }
        return result;
    }
}
